//
//  SongSuoViewController.m
//  XiangMu
//
//  Created by 康世标 on 15/9/28.
//  Copyright (c) 2015年 康世标. All rights reserved.
//

#import "SongSuoViewController.h"

@interface SongSuoViewController ()

@end

@implementation SongSuoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.tableView1.backgroundColor = CL_efeff4;
    self.tableView1.separatorColor = CL_efeff4;
    self.navigationItem.title = @"搜索陪护师";
    self.tabBarController.tabBar.hidden = YES;
    [self jingWeiDu];
    [self souSuo];
    
}
- (void)souSuo{
    
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"数据加载中";
    [QingQiuShuJu souSuoShuJu:self.context block:^(NSDictionary *dic, BOOL isOK) {
        
        NSLog(@"---%@",dic);
        if (isOK) {
            
      
            
            if ([[dic valueForKey:@"status"] intValue] ==200) {
                self.datasArr = [dic valueForKey:@"data"];
                
                [hud removeFromSuperview];
                [self.tableView1 reloadData];
            }
            else{
           
                hud.mode = MBProgressHUDModeText;
                hud.labelText = [dic valueForKey:@"msg"];
                [hud hide:YES afterDelay:2];
                hud.removeFromSuperViewOnHide = YES;
                if ([[dic valueForKey:@"status"] intValue]==202) {
                    
                    
                    [self performSelector:@selector(bick) withObject:self afterDelay:2];
                    
                    
                }
            }
            
        }
        else{
            
            hud.mode = MBProgressHUDModeText;
            hud.labelText = @"网络连接失败";
            [hud hide:YES afterDelay:1];
            hud.removeFromSuperViewOnHide = YES;
        }

    
    }];
}
//获取经纬度
- (void)jingWeiDu{
    [[CCLocationManager shareLocation ] getLocationCoordinate:^(CLLocationCoordinate2D locationCorrrdinate) {
        
        _longitude=locationCorrrdinate.longitude;
        _latitude = locationCorrrdinate.latitude;
        if (_latitude ==0||_longitude ==0) {
            
            [[[UIAlertView alloc]initWithTitle:nil message:@"定位失败" delegate:nil cancelButtonTitle:nil otherButtonTitles: @"取消",nil]show];
            return ;
        }
        
        [self.tableView1 reloadData];
    }];
}

#pragma mark -- UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datasArr.count;
    

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PeiHuShiTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"peihushi"];
    if (!cell) {
        cell=[[[NSBundle mainBundle]loadNibNamed:@"PeiHuShiTableViewCell" owner:nil options:nil]lastObject];
        
    }
    [cell shuJu:self.datasArr index:indexPath.row lat:self.latitude longitude:self.longitude];
    
    
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 116;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //    判断有没有登陆
    if ([UserManager login])
    {
        
        DengLuViewController *d=[[DengLuViewController alloc]init];
        UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:d];
        
        [self.navigationController presentViewController:nav animated:YES completion:nil];
        
        return;
    }
    
  
        NSDictionary * dic =_datasArr[indexPath.row];
        
        
        if ([[dic valueForKey:@"state"] intValue]==1)
        {
            //闲
            XiaDanViewController * x = [[XiaDanViewController alloc]init];
            x.dic = _datasArr[indexPath.row];
            [self.navigationController pushViewController:x animated:YES];
            return;
        }
        else{
            
            [[[UIAlertView alloc]initWithTitle:nil message:@"当前陪护师正在陪护中" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil]show];
            
            return;
            
        }
        
        
        
  
    
}
- (void)bick{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
